package com.example.qts.SQL;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

import com.example.qts.bean.User;

import java.util.ArrayList;
import java.util.List;

public class MySql extends SQLiteOpenHelper {
    private  Context context;

    public Context getContext() {
        return context;
    }

    public void setContext(Context context) {
        this.context = context;
    }

    public MySql(Context context) {
        super(context,"school.db",null,1);
        this.context = context;
    }

    @Override
    public void onCreate(SQLiteDatabase sqLiteDatabase) {
        sqLiteDatabase.execSQL("create table teacher(name varchar(20),age integer,sex varchar(20),id integer)");
    }
    //插入
    public void insertTeacher(String name,int age,String sex,int id){
        MySql mySql = new MySql(context);
        SQLiteDatabase db = mySql.getWritableDatabase();
        ContentValues values = new ContentValues();
        values.put("name",name);
        values.put("age",age);
        values.put("id",id);
        db.insert("teacher",null,values);
        mySql.close();
    }
    //删除(根据id删除)
    public void deleteTeacher(int id){
        MySql mySql = new MySql(context);
        SQLiteDatabase writableDatabase = mySql.getWritableDatabase();
        writableDatabase.delete("teacher","id=?",new String[]{id+""});
        writableDatabase.close();
    }
    //更改
    public void updataTeacher(String newName,String newSex,int newage,int oldld){
        MySql mySql = new MySql(context);
        SQLiteDatabase writableDatabase = mySql.getWritableDatabase();
        ContentValues values = new ContentValues();
        values.put("age" ,newage);
        values.put("name",newName);
        values.put("sex",newSex);
        writableDatabase.update("teacher",values,"id=?",new String[]{oldld+""});
        mySql.close();
    }
    public List<User> btQueryTeacher(){
        MySql mySql = new MySql(context);
        SQLiteDatabase writableDatabase = mySql.getWritableDatabase();
        ArrayList<User> users = new ArrayList<>();
        Cursor teacher = writableDatabase.query("teacher", null, null, null, null, null, null);
        while (teacher.moveToNext()){
            int id = teacher.getInt(teacher.getColumnIndex("id"));
            int age = teacher.getInt(teacher.getColumnIndex("age"));
            String name = teacher.getString(teacher.getColumnIndex("name"));
            String sex = teacher.getString(teacher.getColumnIndex("sex"));
            users.add(new User(id,age,sex,name));
        }
        writableDatabase.close();
        return users;
    }
    public List<User> eqTeacher(int id){
        MySql mySql = new MySql(context);
        SQLiteDatabase readableDatabase = mySql.getReadableDatabase();
        Cursor cursor = readableDatabase.rawQuery("select * from user where id=?", new String[]{id + ""});
       // readableDatabase.rawQuery("select*from user where name=?",new String[]{name+""});
       // readableDatabase.replace("select*from user where like %name%",new String(){name+""});
        ArrayList<User> users = new ArrayList<>();
        if (cursor.moveToNext()){
            String name = cursor.getString(cursor.getColumnIndex("name"));
            String sex = cursor.getString(cursor.getColumnIndex("sex"));
            int id1 = cursor.getInt(cursor.getColumnIndex("id"));
            int age = cursor.getInt(cursor.getColumnIndex("age"));
            users.add(new User(id1,age,name,sex));
        }
        return users;
    }
    @Override
    public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {

    }
}
